package com.red.wine.sql;

import java.util.ArrayList;
import java.util.List;

import com.red.wine.model.TasteBySql;
import com.red.wine.model.TasteInfo;
import com.red.wine.util.StringUtil;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

public class HisSqlHelper {
	private SQLiteDatabase db = null;
	
	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}
	

	public synchronized void addClientInfo(TasteInfo at, String thumb_640, String thumb_64,String thumb_m, String thumb) {
		if (at == null) {
			return;
		}
		String strSql = "insert or replace into myfour(id,title,content,type," +
				"cat,thumbnail_640,thumbnail_64,thumbnail_m,thumb,wine_type,wine_origin,wine_container," +
				"wine_degree,author) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			stm.bindString(1, StringUtil.isEmptyOrNull(at.getId())? "": at.getId());
			stm.bindString(2, StringUtil.isEmptyOrNull(at.getTitle())? "": at.getTitle());
			stm.bindString(3, StringUtil.isEmptyOrNull(at.getContent())? "":at.getContent());
			stm.bindString(4, StringUtil.isEmptyOrNull(at.getType())? "":at.getType());
			stm.bindString(5, StringUtil.isEmptyOrNull(at.getCat())? "":at.getCat());
			
			stm.bindString(6, StringUtil.isEmptyOrNull(thumb_640)? "":thumb_640);
			stm.bindString(7, StringUtil.isEmptyOrNull(thumb_64)? "":thumb_64);
			stm.bindString(8, StringUtil.isEmptyOrNull(thumb_m)? "": thumb_m);
			stm.bindString(9, StringUtil.isEmptyOrNull(thumb)? "": thumb);
			stm.bindString(10, StringUtil.isEmptyOrNull(at.getWine_type())? "":at.getWine_type());
			stm.bindString(11, StringUtil.isEmptyOrNull(at.getWine_origin())? "":at.getWine_origin());
			stm.bindString(12, StringUtil.isEmptyOrNull(at.getWine_container())? "":at.getWine_container());
			stm.bindString(13, StringUtil.isEmptyOrNull(at.getWin_degree())? "":at.getWin_degree());
			stm.bindString(14, StringUtil.isEmptyOrNull(at.getAuthor())? "":at.getAuthor());
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			// will not happen
		}
	}
	
	public List<TasteBySql> getClientInfo() {
		String sql ="select _id,id,title,content,type," +
				"cat,thumbnail_640,thumbnail_64,thumbnail_m,thumb,wine_type,wine_origin," +
				"wine_container," +
				"wine_degree,author from myfour";
		Cursor cursor = db.rawQuery(sql, null);
		TasteBySql historie = null;
		List<TasteBySql> nums = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<TasteBySql>();
				 
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new TasteBySql();
					historie.setId(cursor.getString(1));
					historie.setTitle(cursor.getString(2));
					historie.setContent(cursor.getString(3));
					historie.setType(cursor.getString(4));
					historie.setCat(cursor.getString(5));
					historie.setThumbnail_640(cursor.getString(6));
					historie.setThumbnail_64(cursor.getString(7));
					historie.setThumbnail_m(cursor.getString(8));
					historie.setThumb(cursor.getString(9));
					historie.setWine_type(cursor.getString(10));
					historie.setWine_origin(cursor.getString(11));
					historie.setWine_container(cursor.getString(12));
					historie.setWin_degree(cursor.getString(13));
					historie.setAuthor(cursor.getString(14));
					nums.add(historie);
					cursor.moveToNext();
				}
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return nums;
	}
	
	public int getCount() {
		String sql = " select _id from myfour";
		Cursor cursor = db.rawQuery(sql, null);
		try {
			if (cursor != null && cursor.moveToFirst()) {
				return cursor.getCount();
			}
		} catch (Exception e) {
			return 0;
		} finally {
			cursor.close();
		}
		return 0;
	}

	public void deleteAttribute(String id) {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from myfour where id="+id);
	}
	public void delete() {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from myfour");
	}
}
